from django_excel_templates import *
from django_excel_templates.color_converter import *
from models import *
from django.http import HttpResponse
from django.contrib.auth.models import User




def xls_all_types(request):
    
    
    test_instance = TestAllBaseTypes(
    BooleanField = True,
    CharField = 'Testing CharField...',
    CommaSeparatedIntegerField = '2,8,9',
    DecimalField = '8.23',
    EmailField = 'chalkdust1012@gmail.com',
    FloatField = float(42),
    IntegerField = 9,
    IPAddressField = '192.168.4.180',
    NullBooleanField = False,
    PositiveIntegerField = 42,
    PositiveSmallIntegerField = 12,
    SlugField = 'testingslugfield',
    SmallIntegerField = 1,
    TextField = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. "  +
                "Integer neque leo, viverra et convallis et, dictum vel quam. "  +
                "Aliquam a quam eros. Suspendisse imperdiet sapien non orci  "  +
                "placerat sed faucibus odio vulputate. Nulla a lacus eros, quis  "  +
                "volutpat metus. Nunc vulputate bibendum lectus vitae malesuada. "  +
                "Mauris hendrerit euismod accumsan. Etiam in eros et nisl ultrices  "  +
                "cursus vel non nisl. Class aptent taciti sociosqu ad litora  "  +
                "torquent per conubia nostra, per inceptos himenaeos. In  "  +
                "pretium nunc sed nisi porta eget gravidaquam aliquam. Aliquam "  +
                "sollicitudin leo eu arcu imperdiet a ultrices erat porttitor.  "  +
                "Etiam rutrum laoreet purus, a viverra odio hendreritsit amet.",
    URLField = 'www.google.com',
    )
    
    test_instance.save()

    testobj = TestAllBaseTypes.objects.all()

    headerstyle = ExcelStyle()
    headerstyle.set_font(font_color='FFFFFF',bold=True)
    headerstyle.set_border(border_color='000000',border_style=5)
    headerstyle.set_pattern(pattern_color='000000',pattern=1)
    bodystyle = ExcelStyle(font_color='C0C0C0')
    altcolorstyle = ExcelStyle(font_color='00000',pattern_color='C0C0C0',pattern=1,)
    
    formatter = ExcelFormatter()
    formatter.addHeaderStyle(headerstyle)
    formatter.addBodyStyle(bodystyle)
    formatter.addAlternateColorStyle(altcolorstyle)
    #formatter.addFormula('IntegerField','SUM') #BUG:  can't parse formula SUM(A0:A0)
    #formatter.addFormula('DateField','COUNT')
    #formatter.addFormula('SmallIntegerField,PositiveIntegerField','MAX')
    #formatter.addFormula('DecimalField','MIN')
    
    type_report = ExcelReport()
    type_report.addSheet("All Types")
    filter = ExcelFilter(exclude='TextField')
   
    type_report.addQuerySet(testobj,REPORT_HORZ,formatter,filter)
    
    response = HttpResponse(type_report.writeReport(),mimetype='application/ms-excel')
    response['Content-Disposition'] = 'attachment; filename=type_test.xls'
    return response


def xls_simple(request):
    
    ## Simple ##
    testobj = Book.objects.all()
    
    formatter = ExcelFormatter()
    simpleStyle = ExcelStyle(vert=2,wrap=1)
    formatter.addBodyStyle(simpleStyle)
    formatter.setWidth('name,category,publish_date,bought_on',3000)
    formatter.setWidth('price',600)
    formatter.setWidth('ebook',1200)
    formatter.setWidth('about',20000)
    
    simple_report = ExcelReport()
    simple_report.addSheet("TestSimple")
    filter = ExcelFilter(order='name,category,publish_date,about,bought_on,price,ebook')
    simple_report.addQuerySet(testobj,REPORT_HORZ,formatter, filter)
    
    response = HttpResponse(simple_report.writeReport(),mimetype='application/ms-excel')
    response['Content-Disposition'] = 'attachment; filename=simple_test.xls'
    return response


def xls_medium(request):
    ## Medium ##
    mediumReport = ExcelReport()
    mediumReport.addSheet("Test Medium")
    mediumReport.set_query_add_direction(QUERY_ADD_DOWN)
    testobj = Book.objects.all()

    headerstyle = ExcelStyle(font_color='FFFFFF',bold=True,pattern_color='000080',pattern=1)
    bodystyle = ExcelStyle(pattern_color='0066CC',pattern=1,vert=2,wrap=1,border_color='000000',border_style=2)
    colstyle = ExcelStyle(pattern_color='FF00FF',pattern=1,vert=2,wrap=1,border_color='FFFFFF',border_style=2)
    
    mediumFormatter = ExcelFormatter()
    mediumFormatter.addHeaderStyle(headerstyle)
    mediumFormatter.addBodyStyle(bodystyle)
    mediumFormatter.addColumnStyle('chapters',colstyle)
    mediumFormatter.setWidth('name,category,publish_date,bought_on',3200)
    mediumFormatter.setWidth('chapters',10000)
    mediumFormatter.setWidth('price',600)
    mediumFormatter.setWidth('ebook',1200)
    mediumFormatter.setWidth('about',20000)
    
    mediumFilter = ExcelFilter(order='name,category,publish_date,about,bought_on,price,ebook,chapters')
    mediumReport.addQuerySet(testobj,REPORT_HORZ,mediumFormatter,mediumFilter)

    response = HttpResponse(mediumReport.writeReport(),mimetype='application/ms-excel')
    response['Content-Disposition'] = 'attachment; filename=medium_test.xls'
    return response

def xls_adv(request):
    advReport = ExcelReport()
    advReport.addSheet("TestMedium")
    advReport.set_query_add_direction(QUERY_ADD_DOWN)
    testobj = Book.objects.all()

    headerstyle = ExcelStyle(font_color='FFFFFF',bold=True,pattern_color='000080',pattern=1)
    bodystyle = ExcelStyle(pattern_color='0066CC',pattern=1,vert=2,wrap=1,border_color='000000',border_style=2)
    colstyle = ExcelStyle(pattern_color='FF00FF',pattern=1,vert=2,wrap=1,border_color='FFFFFF',border_style=2)
    
    advFormatter = ExcelFormatter()
    advFormatter.addHeaderStyle(headerstyle)
    advFormatter.addBodyStyle(bodystyle)
    advFormatter.addColumnStyle('chapters',colstyle)
    advFormatter.setWidth('name,category,publish_date,bought_on',3200)
    advFormatter.setWidth('chapters',10000)
    advFormatter.setWidth('price',600)
    advFormatter.setWidth('ebook',1200)
    advFormatter.setWidth('about',20000)
    
    #advFilter = ExcelFilter(order='name,category,publish_date,about,bought_on,price,ebook,chapters')
    advFilter = ExcelFilter(order='name,category,ebook,chapters')   
    advReport.addQuerySet(testobj,REPORT_HORZ,advFormatter,advFilter)

    testobj2 = Book.objects.filter(price__gt=10)

    headerstyle2 = ExcelStyle(font_color='FFFFFF',bold=True,pattern_color='800000',pattern=1)
    bodystyle2 = ExcelStyle(pattern_color='6600CC',pattern=1,vert=2,wrap=1,border_color='000000',border_style=2)
    colstyle2 = ExcelStyle(pattern_color='00FFFF',pattern=1,vert=2,wrap=1,border_color='FFFFFF',border_style=2)
    
    advFormatter2 = ExcelFormatter()
    advFormatter2.addHeaderStyle(headerstyle2)
    advFormatter2.addBodyStyle(bodystyle2)
    advFormatter2.addColumnStyle('chapters',colstyle2)
    advFormatter2.setWidth('name,category,publish_date',4200)
    advFormatter2.setWidth('chapters',10000)
    advFormatter2.setWidth('price',600)
    advFormatter2.setWidth('ebook',1200)
    advFormatter2.setWidth('about',20000)
    
    advFilter2 = ExcelFilter(order='name,category,publish_date,about,price,ebook,chapters')
    advReport.addQuerySet(testobj2,REPORT_HORZ,advFormatter2,advFilter2)

    response = HttpResponse(advReport.writeReport(),mimetype='application/ms-excel')
    response['Content-Disposition'] = 'attachment; filename=adv_test.xls'
    return response

def xls_adv_bug_fill_cols(request):
    advReport = ExcelReport()
    advReport.addSheet("TestMedium")
    advReport.set_query_add_direction(QUERY_ADD_DOWN)
    testobj = Book.objects.all()[0:1]
    
    advFilter = ExcelFilter(order='name,chapters')   
    advReport.addQuerySet(testobj,REPORT_HORZ,filter=advFilter)

    testobj2 = Book.objects.filter(price__gt=10)[0:1]

    headerstyle2 = ExcelStyle(font_color='FFFFFF',bold=True,pattern_color='800000',pattern=1)
    bodystyle2 = ExcelStyle(pattern_color='6600CC',pattern=1,vert=2,wrap=1,border_color='000000',border_style=2)
    advFormatter2 = ExcelFormatter()
    advFormatter2.addHeaderStyle(headerstyle2)
    advFormatter2.addBodyStyle(bodystyle2)
    
    advFilter2 = ExcelFilter(order='name,category,publish_date,chapters')
    advReport.addQuerySet(testobj2,REPORT_HORZ,advFormatter2,advFilter2)

    response = HttpResponse(advReport.writeReport(),mimetype='application/ms-excel')
    response['Content-Disposition'] = 'attachment; filename=adv_bug_fill_cols_test.xls'
    return response

   
def Break():

    try: 
        import dbgp.client
        dbgp.client.brk(port=9000)
    except:
        print "Count not Break. To fix: insert a debug client into Break() function"
        pass
    
